<template>
	<div class="app-container" v-loading="loading">
		<div class="search-container  app_ny">
			<div class="div2" style="position: relative">
				<el-row :gutter="12" style="width: 100%">
					<el-col :span="3">
						<el-input v-model="queryParams.keyword" clearable placeholder="请输入关键词"
							@keyup.enter="handleQuery" />
					</el-col>
					<el-col :span="3">
						<el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable>
							<el-option label="审核中" value="0" />
							<el-option label="已通过" value="1" />
							<el-option label="已驳回" value="2" />
						</el-select>
					</el-col>
					<el-col :span="6">
						<el-button :loading="loading" type="primary" @click="handleQuery"><template
								#icon><i-ep-search /></template>查询</el-button>
					</el-col>
				</el-row>
				<FilterColumn class="filtBox" @handleRefresh="resetQuery()" :strtype="strtype"></FilterColumn>
			</div>
		</div>
		<el-card shadow="never">
			<div class="action-row">
				<el-button @click="handleDo('', 'act_info')" :disabled="multipleSelection.length !== 1"
					:type="buttonType(multipleSelection.length)" plain><template
						#icon><i-ep-document /></template>详情</el-button>
				<el-button v-hasPerm="['AuditCheck']" @click="handleDo('', 'act_audit')"
					:disabled="multipleSelection.length !== 1 || multipleSelection[0]?.status != 0"
					:type="buttonType(multipleSelection.length)" plain><template
						#icon><i-ep-star /></template>审核</el-button>
			</div>
			
			<el-table ref="dataTableRef" v-loading="loading" :data="tableData" align="left" tooltip-effect="dark" border
				:show-overflow-tooltip="true" @cell-mouse-enter="rowMouse" @cell-mouse-leave="menuLineVisible=false"
				@selection-change="handleSelectionChange">
				<el-table-column type="selection" align="center" width="55" />
				<el-table-column type="index" label="序号" align="center" width="80" />
				<template v-for="(item,index) in cColumnsList()" :key='index'>
					<el-table-column :label="item.label" :align="item.align" :prop="item.prop" :min-width="item.width">
						<template #default="scope" v-if="item.prop == 'audit[0].files'">
							<template v-if="scope.row.audit[0].files.length>0">
								<el-link @click="handleDo(scope.row, 'act_info')"  type="primary">
									{{scope.row.audit[0].files.length}}<template
										#icon><i-ep-link /></template>附件</el-link>
							</template>
							<el-link v-else type="info">无</el-link>
						</template>
						<template #default="scope" v-if="item.prop == 'status'">
							<el-tag
								:type="titModel[scope.row.status].type">{{titModel[scope.row.status].title}}</el-tag>
						</template>
					</el-table-column>
				</template>
			</el-table>
			<pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.page"
				v-model:limit="queryParams.limit" @pagination="handleQuery" />
		</el-card>

		<!-- 编辑 or 添加 -->
		<aggravateMinePendingAuditing v-if="dialogAdd.visible" v-model="dialogAdd.visible"
			:dialogTitle="dialogAdd.title" :dialogTableValue="dialogAdd.value" :dialogStatus="dialogAdd.status"
			@initRefreshList="handleQuery">
		</aggravateMinePendingAuditing>



	</div>
</template>
<script setup lang="ts">
	import { useRoute } from 'vue-router';
	import _ from 'lodash-es'
	import { useUserStore } from "@/store/modules/user";
	import { useTableFilterStore } from "@/store/modules/tableFilter";
	import aggravateMinePendingAuditing from "./components/aggravateMinePendingAuditing.vue";
	import { BUSINESS_TYPE } from "@/utils/constant"
	import {
		getAuditCheck_list
	} from "@/api/aggravate";

	const TableFilter = useTableFilterStore();
	const strtype = ref('aggravateMinePending'); //数据
	const userStore = useUserStore();

	// 表格标题
	const cTitles = ["工程名称", "业务名称", "所属子公司/部门", "经办人", "申请内容", "附件", "审核状态", "审核备注"];
	// 显示字段
	const sTitles = ["工程名称", "业务名称", "所属子公司/部门", "经办人", "审核状态", "审核备注"];
	// 字段属性
	const cColumns = [
	
		{ label: '工程名称', prop: 'audit[0].engine.name', align: 'center', minwidth: 200 },
		{ label: '业务名称', prop: 'audit[0].name', align: 'center', minwidth: 200 },
		{ label: '所属子公司/部门', prop: 'audit[0].department.name', align: 'center', minwidth: 200 },
		{ label: '经办人', prop: 'audit[0].user.name', align: 'center', minwidth: 150 },
		{ label: '申请内容', prop: 'audit[0].content', align: 'center', minwidth: 200 },
		{ label: '附件', prop: 'audit[0].files', align: 'center', minwidth: 150 },
		{ label: '审核状态', prop: 'status', align: 'center', minwidth: 150 },
		{ label: '审核备注', prop: 'mark', align: 'center', minwidth: 200 },
	]
	interface TableGlobalLocal {
		[key : string] : any; // 或者更具体的类型
	}
	// 列表渲染
	function cColumnsList() {
		const ColumnsList = (TableFilter.tableGlobalLocal as TableGlobalLocal)[strtype.value + '_checklist'];
		if (ColumnsList) {
			const columnMap : any = {}; // 创建一个对象来存储cColumns中label和对象的映射  
			// 预先建立label到对象的映射  
			for (const column of cColumns) {
				columnMap[column.label] = column;
			}
			const newList = [];
			for (const label of ColumnsList) {
				// 尝试从映射中获取对象  
				const foundColumn = columnMap[label];
				if (foundColumn) {
					// 如果找到了，则添加到newList中  
					newList.push(foundColumn);
				}
			}
			return newList;
		}
		else {
			return [];
		}
	};
	// 列表-数据列初始化
	function columnsInit() {
		const dropColStr : string = strtype.value + '_dropCol';
		const checklistStr : string = strtype.value + '_checklist';
		const drop_col : any = (TableFilter.tableGlobalLocalTemp as TableGlobalLocal)[dropColStr];
		const checklist = (TableFilter.tableGlobalLocalTemp as TableGlobalLocal)[checklistStr];
		if (!drop_col) {
			(TableFilter.tableGlobalLocal as TableGlobalLocal)[dropColStr] = JSON.parse(JSON.stringify(cTitles));
			(TableFilter.tableGlobalLocalTemp as TableGlobalLocal)[dropColStr] = JSON.parse(JSON.stringify(cTitles));
		}
		if (!checklist) {
			(TableFilter.tableGlobalLocal as TableGlobalLocal)[checklistStr] = JSON.parse(JSON.stringify(sTitles));
			(TableFilter.tableGlobalLocalTemp as TableGlobalLocal)[checklistStr] = JSON.parse(JSON.stringify(sTitles));
		}
	}


	// 数据的data-ref
	const queryFormRef = ref(ElForm);
	/*** 搜索窗-字段表 */
	const queryParams = reactive({
		page: 1,
		limit: 20,
		keyword: "",
		status: ""

	});

	const loading = ref(false);//页面加载load
	const total = ref(0);
	const tableData = ref([])
	const multipleSelection =ref<any>([]);
	const menuLineVisible =ref(true);
	const rowR = ref<any>({});
	
	
	const dialogAdd = reactive<any>({
		title: "添加信息", //弹窗标题
		visible: false,   //弹窗显示or隐藏
		status: "add_art", //弹窗状态类型  :添加 add_art 编辑 edit_art 详情 detail_art  
		value: {}   		  //携带整条参数 row
	});

	const titModel = ref([{
		title: '待审核',
		type: 'info',
		value: 0
	}, {
		title: '已通过',
		type: 'success',
		value: 1
	}, {
		title: '已驳回',
		type: 'danger',
		value: 2
	}])

	// 行菜单事件
	function rowMouse(row : any) {
		menuLineVisible.value = true
		rowR.value = row
	}
	// 数据多选事件
	function handleSelectionChange(val : any) {
		multipleSelection.value = val
	}
	// 操作按钮样式
	function buttonType(val : number) {
		if (val === 1) {
			return 'primary'
		} else {
			return 'info'
		}
	}
	
	// 操作
	function handleDo(row:any, act:string) {
		dialogAdd.status = act
		switch (act) {
			case 'act_audit':
				var rowItem= JSON.parse(JSON.stringify(row || multipleSelection.value[0]))
				dialogAdd.visible = true;
				dialogAdd.title = '工单审核';
				dialogAdd.value = rowItem;
				break
			case 'act_info':
				var rowItem= JSON.parse(JSON.stringify(row || multipleSelection.value[0]))
				dialogAdd.visible = true;
				dialogAdd.title = '修改信息';
				dialogAdd.value = rowItem;
				break
			default:	
		}
	}   


	// 初始化搜索内容
	function handleQuery() {
		loading.value = true;
		getAuditCheck_list(queryParams).then((res) => {
			tableData.value = res.data.list
			total.value = res.data.count
		}).finally(() => {
			loading.value = false;
		})
	}
	// 重置搜索内容
	function resetQuery() {
		handleQuery();
	}
	onMounted(() => {
		handleQuery();
		columnsInit();

	});
</script>

<style lang="scss" scoped>

</style>